//
//  WSAddCVDetailCell.m
//  LANGirl
//
//  Created by 吴帅 on 16/3/19.
//
//

#import "WSAddCVDetailView.h"

#define MAINBACKCOLOR [UIColor colorWithRed:242/255.0 green:242/255.0 blue:242/255.0 alpha:1]

@interface WSAddCVDetailView ()<UITextViewDelegate>

@property (nonatomic, strong) UILabel *titleLabel;
@property (nonatomic, strong) UITextView *textView;

@end

@implementation WSAddCVDetailView

- (void)dealloc {
    
    [[NSNotificationCenter defaultCenter] removeObserver:self name:@"UITextViewTextDidChangeNotification" object:_textView];
}

- (instancetype)initWithFrame:(CGRect)frame {
    
    self = [super initWithFrame:frame];
    if (self) {
        self.backgroundColor = [UIColor whiteColor];
        [self createSubView];
        [self createDone];
    }
    return self;
}

- (void)createSubView {
    
    self.titleLabel = [[UILabel alloc] init];
    [self addSubview:_titleLabel];
    [self.titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.right.top.mas_equalTo(@0);
        make.left.mas_equalTo(15.00 * AUTO_WIDTH);
        make.height.mas_equalTo(40 * AUTO_WIDTH);
    }];
    self.titleLabel.font = [UIFont systemFontOfSize:TEXTCONTENTFONT];
    self.titleLabel.text = @"任务详情";
    self.titleLabel.textColor = HS;
    
    self.textView = [[UITextView alloc] init]; //初始化大小并自动释放
    self.textView.textColor = [UIColor grayColor];//设置textview里面的字体颜色
    self.textView.font = [UIFont fontWithName:@"Arial" size:TEXTCONTENTFONT * TEXTWEIGHT];//设置字体名字和字体大小
    self.textView.delegate = self;//设置它的委托方法
    self.textView.layer.borderWidth = 2;
    self.textView.layer.cornerRadius = 3;
    self.textView.layer.borderColor = MAINBACKCOLOR.CGColor;
    self.textView.backgroundColor = [UIColor whiteColor];//设置它的背景颜色
    self.textView.returnKeyType = UIReturnKeyDefault;//返回键的类型
    self.textView.keyboardType = UIKeyboardTypeDefault;//键盘类型
    self.textView.scrollEnabled = YES;//是否可以拖动
    self.textView.autoresizingMask = UIViewAutoresizingFlexibleHeight;//自适应高度
    [self addSubview: self.textView];//加入到整个页面中
    [self.textView mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.equalTo(_titleLabel.mas_bottom);
        make.left.mas_equalTo(@(15 * AUTO_WIDTH));
        make.right.mas_equalTo(@(-15 * AUTO_WIDTH));
        make.bottom.mas_equalTo(@(-70 * AUTO_WIDTH));
    }];
    
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    button.backgroundColor = GH;
    button.layer.masksToBounds = YES;
    button.layer.cornerRadius = 5;
    [button setTitle:@"发布任务" forState:UIControlStateNormal];
    [button addTarget:self action:@selector(finish:) forControlEvents:UIControlEventTouchUpInside];
    [button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [button setTitleColor:HS forState:UIControlStateNormal];
    button.frame = CGRectMake(50.00*AUTO_WIDTH, 600.00*AUTO_WIDTH, 275.00*AUTO_WIDTH, 40.00*AUTO_WIDTH);
    button.titleLabel.font = [UIFont fontWithName:@"Arial" size:14.00*AUTO_WIDTH];
    [self addSubview:button];
    
    [button mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.left.mas_equalTo(@(15 * AUTO_WIDTH));
        make.right.mas_equalTo(@(-15 * AUTO_WIDTH));
        make.top.equalTo(_textView.mas_bottom).offset(15 * AUTO_WIDTH);
        make.bottom.mas_equalTo(@(-15 * AUTO_WIDTH));
    }];
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(textFiledEditChanged:)
                                                 name:@"UITextViewTextDidChangeNotification" object:self.textView];
}

- (void)createDone {

    UIToolbar * topView = [[UIToolbar alloc]initWithFrame:CGRectMake(0, 0, 320, 30)];
    
    [topView setBarStyle:UIBarStyleDefault];
    
    UIBarButtonItem * btnSpace = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:self action:nil];
    
    UIBarButtonItem * doneButton = [[UIBarButtonItem alloc]initWithTitle:@"完成" style:UIBarButtonItemStyleDone target:self action:@selector(dismissKeyBoard)];
    
    NSArray * buttonsArray = [NSArray arrayWithObjects:btnSpace,doneButton,nil];
    
    [topView setItems:buttonsArray];
    
    [_textView setInputAccessoryView:topView];
}

- (void)finish:(UIButton *)btn {

    [self.delegate finishAction];
}

-(void)dismissKeyBoard{
    
    [_textView resignFirstResponder];
}

- (BOOL)textViewShouldBeginEditing:(UITextView *)textView {

    [self.delegate changeViewOffset:YES textView:_textView];
    
    return YES;
}

- (BOOL)textViewShouldEndEditing:(UITextView *)textView {

    [self.delegate changeViewOffset:NO textView:_textView];
    
    return YES;
}

- (void)setTitleName:(NSString *)titleName {

    _titleName = titleName;
    self.titleLabel.text = _titleName;
}

#pragma mark - Notification Method
-(void)textFiledEditChanged:(NSNotification *)obj
{
    UITextField *textField = (UITextField *)obj.object;
    NSString *toBeString = textField.text;
    NSString *lang = [textField.textInputMode primaryLanguage];
    if ([lang isEqualToString:@"zh-Hans"])// 简体中文输入
    {
        //获取高亮部分
        UITextRange *selectedRange = [textField markedTextRange];
        UITextPosition *position = [textField positionFromPosition:selectedRange.start offset:0];
        
        // 没有高亮选择的字，则对已输入的文字进行字数统计和限制
        if (!position)
        {
            if (toBeString.length > _maxTextNum)
            {
                textField.text = [toBeString substringToIndex:_maxTextNum];
            }
        }
        
    }
    // 中文输入法以外的直接对其统计限制即可，不考虑其他语种情况
    else
    {
        if (toBeString.length > _maxTextNum)
        {
            NSRange rangeIndex = [toBeString rangeOfComposedCharacterSequenceAtIndex:_maxTextNum];
            if (rangeIndex.length == 1)
            {
                textField.text = [toBeString substringToIndex:_maxTextNum];
            }
            else
            {
                NSRange rangeRange = [toBeString rangeOfComposedCharacterSequencesForRange:NSMakeRange(0, _maxTextNum)];
                textField.text = [toBeString substringWithRange:rangeRange];
            }
        }
    }
}

- (void)setContentStr:(NSString *)contentStr {

    if (_contentStr != contentStr) {
        
        _contentStr = contentStr;
    }
    self.textView.text = _contentStr;
}

@end
